<?php
class User {
	
	private $userID;
	private $username;
	private $level;
	private $health;
	private $attack;
	private $defence;
	private $experience;
	private $money;
	private $energy;
	private $wins;
	private $looses;
	private $lockedPeriod;
	private $lockedTime;
	private $compBattles;
	private $questionsReset;
	private $energy_restore;
	
	
	public function __construct($userID) {
		
		$userData = DBUtils::getUserData($userID);
		$this->userID = $userData['user_id'];
		$this->username = $userData['user_name'];
		$this->level = $userData['level'];
		$this->experience = $userData['exp'];
		$this->money = $userData['money'];
		$this->health = $userData['health'];
		$this->energy = $userData['energy'];
		$this->wins = $userData['wins'];
		$this->looses = $userData['looses'];
		$this->lockedPeriod = $userData['locked_period'];
		$this->lockedTime = $userData['locked_time'];
		$this->compBattles = $userData['comp_battles'];
		$this->questionsReset = $userData['questions_reset'];
		$this->attack = $userData['attack'];
		$this->defence = $userData['defence'];
		$this->energy_restore = $userData['energy_restore'];
	}
	
	public function getUsername() {
		
		return $this->username;	
	}
	
	public function getUserID() {
		
		return $this->userID;
	}
	
	public function setLevel($level) {	
		
		if(!is_int($level)) {
			
			throw new Exception("Level value should be an integer!");
		}
		
		if($level< $this->level && $this->username != "bot") {
			
			throw new Exception("Level value should be greater than the prevous level!");
		}
		
		$this->level = $level;
		$this->updateUserData();
	}
	
	public function getLevel() {
		
		return $this->level;
	}
	
	public function setHealth($health){
		
		if(!is_numeric($health)) {
			throw new Exception("Invalid value for health!");
		}
		
		$this->health = $health;
		$this->updateUserData();
	}
	
	public function getHealth(){
		
		return $this->health;
	}
	
	public function setAttack($attack) {
		
		if(!is_numeric($attack)) {
			
			throw new Exception("Invalid value for attack!");
		}
		if($attack < 0) {
			
			throw new Exception("Attack can't have negative value!");
		}
		
		$this->attack = $attack;
		$this->updateUserData();
	}
	
	public function getAttack() {
		
		return $this->attack;
		
	}
	
	public function setDefence($defence) {
		
		if(!is_numeric($defence)) {
			
			throw new Exception("Invalid value for defence!");
			
		}
		if($defence < 0) {	
			
			throw new Exception("Defence can't have negative value!");
			
		}
		
		$this->defence = $defence;
		$this->updateUserData();
	}
	
	public function getDefence() {	
		
		return $this->defence;
	}
	
	public function setExperience($experience) {
		
		if(!is_int($experience)) {
			
			throw new Exception("Invalid value for experience!");
		}
		
		if($experience < 0) {	
			
			throw new Exception("Experience can't have a negative value!");
			
		}
		
		$this->experience = $experience;
		$this->updateUserData();
	}
	
	public function getExperience() {	
		
		return $this->experience;
		
	}

	public function setMoney($money) {
		
		if(!is_int($money)) {
			throw new Exception("Invalid value for money!");
		}
		if($money < 0) {
			throw new Exception("Money can't have a negative value!");
		}
		$this->money = $money;
		$this->updateUserData();
	}
	
	public function getMoney() {	
		
		return $this->money;
	}
	
	public function setEnergy($energy) {
		
		if(!is_int($energy)) {
			
			throw new Exception("Invalid value for energy!");
		}
		
		if($energy < 0) {
			
			throw new Exception("Energy can't have a negative value!");
		}
		
		if($energy > 6) {
			
			$energy = 6;
		}
		
		$this->energy = $energy;
		$this->updateUserData();
	}
	
	public function getEnergy() {
		
		return $this->energy;
	}
	
	public function setWins(){	
			
		$this->wins += 1;
		$this->updateUserData();
	}
	
	public function getWins(){		
		
		return $this->wins;
	}
	
	public function setLooses(){	
		
		$this->looses += 1;
		$this->updateUserData();
	}
	
	public function getLooses(){		
		
		return $this-looses;
	}
	
	public function setLockedPeriod($lockedPeriod) {	
		
		if(!is_int($lockedPeriod)) {
			
			throw new Exception("Iccorrect value for locked period!");
		}
		
		if($lockedPeriod < 0) {
			throw new Exception("Locked  period can't have a negative value!");
		}
		
		$this->lockedPeriod = $lockedPeriod;
		$this->updateUserData();
	}
	
	public function getLockedPeriod() {	
		
		return $this->lockedPeriod;
	}
	
	public function setLockedTime(DateTime $date) {	
		
		$today = new DateTime();
		
		if(get_class($date) != "DateTime") {		
			throw new  Exception("Input for locked time should be a DateTime object!");
		} 
		
		if($date<$today) {
			throw new  Exception("Locked time can`t be date in the past.");
		}
		
		$this->lockedTime = $date->format('Y-m-d H:i:s');
		$this->updateUserData();
	}
	
	public function getLockedTime() {
		
		return $this->lockedTime;
		
	}
	
	public function setCompBattles() {

		if($this->compBattles <=0) {
			
			throw new Exception("You don't have any more attempts today!");
		}
		$this->compBattles--;
		$this->updateUserData();
	}
	
	public function getCompBattles() {
		
		return $this->compBattles;
	}
	
	public function setQuestionsReset($bool) {
		
		if($bool != 1 && $bool !=0) {
			
			throw new Exception("Invalid value for questions reset!");
		}
		
		$this->updateUserData();
	}
	
	public function getQuestionsReset() {
	
		return $this->questionsReset;
	}
	
	public function getRank(){
		
		if($this->wins == 0 && $this->looses == 0) {
			
			return 0;
			
		} else {
			
			$rank = round($this->wins/($this->wins+$this->looses),2) * 100;
			
			return $rank;
		}
	}
	
	public function setEnergyRestore(){
		$today = new DateTime();
		
		if(get_class($date) != "DateTime") {
			throw new  Exception("Input for locked time should be a DateTime object!");
		}
		
		if($date<$today) {
			throw new  Exception("Locked time can`t be date in the past.");
		}
		
		$this->lockedTime = $date->format('Y-m-d H:i:s');
		$this->updateUserData();
	}
	
	public function getEnergyRestore(){
		return $this->EnergyRestore;
	}

	
	private function updateUserData() {
		
		$userData = array();
		$userData['user_id'] = $this->userID;
		$userData['level'] = $this->level;
		$userData['exp'] = $this->experience;
		$userData['money'] = $this->money;
		$userData['health'] = $this->health;
		$userData['energy'] = $this->energy;
		$userData['wins'] = $this->wins;
		$userData['looses'] = $this->looses;
		$userData['locked_period'] = $this->lockedPeriod;
		$userData['locked_time'] = $this->lockedTime;
		$userData['comp_battles'] = $this->compBattles;
		$userData['questions_reset'] = $this->questionsReset;
		$userData['attack'] = $this->attack;
		$userData['defence'] = $this->defence;
		$userData['energy_restore'] = $this->energy_restore;
		DBUtils::updateUserData($userData);
	}
	
	public function checkIfLocked() {
		
		$lockedTime = new DateTime($this->lockedTime);	
		$lockedEndingTime = $lockedTime->modify("+".$this->lockedPeriod." minutes");
		$now = new DateTime();
		if($lockedEndingTime<$now) {	
			
			return false;
		}
		if($lockedEndingTime>$now) {	
			
			return true;
		}
	}
}